import Taro, {Component} from '@tarojs/taro'
import { AtFab ,AtList, AtListItem } from 'taro-ui'
import {getGlobalData, setGlobalData} from "../../../global.d";
import {getLocation,getDistance,toast,getFormatDate} from "../../../utils/index";

import {View} from "@tarojs/components"
import './index.scss'
const db = Taro.cloud.database();
export default class Index extends Component {
  config = {
    enablePullDownRefresh:true
  }
    constructor(props) {
        super(props);
        this.state = {
            current: 0,
            user:{
              _id:'',
              userLastTimeClass:{
              type:''
              }},
            breakList:[]
        }
    }
  componentWillMount(){
    Taro.setNavigationBarTitle({
      title: '请假'
    })
    const user = getGlobalData('user')
    this.setState({
      user
    })
  }
  componentDidMount () {

  }
  componentDidShow(){
    this.getList()
  }
  onPullDownRefresh(){
    this.getList()
  }

    addNew(){
      Taro.navigateTo({
        url:'/pages/tools/askForBreak/addNew/index'
      })
    }
    getList(){
      db.collection("break")
        .where({
          classKey:this.state.user.userLastTimeClass.classKey
        })
        .get()
        .then(res=>{
          let breakList = res.data
          breakList.reverse()
         this.setState({
           breakList
         })
        })
    }

    render() {
      const userType = this.state.user.userLastTimeClass.type
      const userId  = this.state.user._id
      const  list = this.state.breakList.map((item,index)=>{
        const time = new Date(item.addTime)
        if(userType==='admin'){
          return <AtListItem
            arrow='right'
            title={item.name}
            note={'提出时间：'+getFormatDate(time)}
            extraText={item.status}
          />
        } else {
          if(item.userId === userId){
            return <AtListItem
              arrow='right'
              title={item.name}
              note={'提出时间：'+getFormatDate(time)}
              extraText={item.status}
            />
          }
        }
      })
        return (
            <View >
              <AtList>
                {list}
              </AtList>
              <View className='AtFab'>
                <AtFab onClick={this.addNew.bind(this)}>
                  <Text className='at-fab__icon at-icon at-icon-add'></Text>
                </AtFab>
              </View>

            </View>
        )
    }

}
